<?php
declare(strict_types=1);

namespace App\Resource\Dynamic\Reply;


use Carbon\Carbon;
use Illuminate\Http\Resources\Json\JsonResource;

class Resource extends JsonResource
{
    public function toArray($request)
    {
        Carbon::setLocale('zh');
        return [
            'id' => $this->id,
            'dynamic_id' => $this->dynamic_id,
            'user_id' => $this->user_id,
            'like_count' => $this->like_count,
            'is_like' => (empty($this->like_reply) || empty($this->like_reply->count())) ? 0 : 1,
            'content' => $this->content,
            'reply_user_id' => $this->reply_user_id,
            'reply_user_nickname' => $this->reply_user->nickname ?? '',
            'status' => $this->status,
            'user' => [
                'id' => $this->user->user_id ?? 0,
                'code' => $this->user->user_data->user_code ?? 0,
                'nickname' => $this->user->user_data->nickname ?? '',
                'sex' => $this->user->user_data->sex ?? 0,
                'head_picture' => $this->user->user_data->head_picture ?? '',
                'level_icon' => $this->user->grade->head_picture?? '',
                'charm_icon' =>  app('model_charm_config')->getCharmIcon($this->user->charm_id ?? 0),
                'decorate' => (!isset($this->user->decorate) || empty($this->user->decorate)) ? (object)[] : $this->user->decorate,
            ],
            'children' => $this->children ? ChildrenResource::collection($this->children) : null,
            'show_at' => Carbon::make($this->created_at)->diffForHumans(),
            'created_at' => Carbon::make($this->created_at)->toDateTimeString(),
        ];
    }
}
